package com.th.supcom.pds.impl.service.adapter;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.th.supcom.pca.common.domain.vo.MsgLog;
import com.th.supcom.pca.common.domain.vo.Result;
import com.th.supcom.pca.patientinfo.domain.pojo.PcaPatientInfo;
import com.th.supcom.pca.patientinfo.domain.vo.PatientSearch;
import com.th.supcom.pca.patientinfo.domain.vo.PcaMediaTrasInfo;
import com.th.supcom.pds.domain.vo.MedicalCardInfoVO;
import com.th.supcom.pds.domain.vo.MedicalCardPatientInfoVO;
import com.th.supcom.pds.domain.vo.PatientInfo;
import com.th.supcom.pds.domain.vo.PcaPatientTrasInfoVO;
import com.th.supcom.pds.impl.common.enums.IDTypeEnum;

public final class ChangeToCardNeedInfo
{

    private ChangeToCardNeedInfo ()
    {

    }

    /**
     * PcaPatientTrasInfo 变成 AttendanceCardPatientInfo
     * 
     * @param pcaPatientTrasInfo
     * @return
     */
    public static PatientInfo turnToAttendanceCard ( Map <String, Object>inmap)
    {       if(inmap==null){
           return null;
       }
        PatientInfo medicalCardPatientInfo = new PatientInfo ();
        medicalCardPatientInfo.setPatiId((String)inmap.get ("patientId"));
        medicalCardPatientInfo.setName ((String)inmap.get ("patiName"));
        medicalCardPatientInfo.setCardNo ((String)inmap.get ("cardNo"));
        medicalCardPatientInfo.setBalance ((Double) inmap.get ("cardBalance"));
        medicalCardPatientInfo.setOutpVisitId ((Long) inmap.get ("outpVisitId"));
        medicalCardPatientInfo.setPrescSource (Integer.parseInt (inmap.get ("outpTypeCode")+""));
        //medicalCardPatientInfo.setNamePhonetic (pcaPatientInfo.getNamePhonetic ());
        medicalCardPatientInfo.setSex((String)inmap.get ("patiGenderName"));
        medicalCardPatientInfo.setAge((String)inmap.get ("patiAgeString"));
        medicalCardPatientInfo.setChargeType((String)inmap.get ("visitTypeName"));
        medicalCardPatientInfo.setVisitId ((Long) inmap.get ("visitId"));
        //medicalCardPatientInfo.setPhoneNumber1 (pcaPatientInfo.getPhoneNumber1 ());
        //medicalCardPatientInfo.setHomeAddrZipCode (pcaPatientInfo.getHomeAddrZipCode ());
        //medicalCardPatientInfo.setNationCode (pcaPatientInfo.getNationCode ());
        //medicalCardPatientInfo.setCitizenshipCode (pcaPatientInfo.getCitizenshipCode ());
       // medicalCardPatientInfo.setOccupationCode (pcaPatientInfo.getOccupationCode ());
        //medicalCardPatientInfo.setPhoneNumber2 (pcaPatientInfo.getPhoneNumber2 ());
       // medicalCardPatientInfo.setHomeAddrProvinceCode (pcaPatientInfo.getHomeAddrProvinceCode ());
        //medicalCardPatientInfo.setHomeAddrCityCode (pcaPatientInfo.getHomeAddrCityCode ());
        //medicalCardPatientInfo.setHomeAddrCountyCode (pcaPatientInfo.getHomeAddrCountyCode ());
        //edicalCardPatientInfo.setHomeAddrStreet (pcaPatientInfo.getHomeAddrStreet ());
       // medicalCardPatientInfo.setBirthAddrProvinceCode (pcaPatientInfo.getBirthAddrProvinceCode ());
        //medicalCardPatientInfo.setBirthAddrCityCode (pcaPatientInfo.getBirthAddrCityCode ());
       // medicalCardPatientInfo.setBirthAddrCountyCode (pcaPatientInfo.getBirthAddrCountyCode ());
        //medicalCardPatientInfo.setBirthAddrStreet (pcaPatientInfo.getBirthAddrStreet ());
        //medicalCardPatientInfo.setNextOfKin (pcaPatientInfo.getNextOfKin ());
        //medicalCardPatientInfo.setNextOfKinPhone1 (pcaPatientInfo.getNextOfKinPhone1 ());
        //medicalCardPatientInfo.setRelationshipCode (pcaPatientInfo.getRelationshipCode ());
        //medicalCardPatientInfo.setNextOfKinZipCode (pcaPatientInfo.getNextOfKinZipCode ());
        //medicalCardPatientInfo.setInsuranceTypeCode (pcaPatientInfo.getInsuranceTypeCode ());
        //medicalCardPatientInfo.setCreateTime (pcaPatientInfo.getCreateTime ());
        //medicalCardPatientInfo.setPaymentCode (pcaPatientInfo.getPaymentCode ());
       /* if (null != pcaPatientInfo.getOtherIdNumber () || "".equals (pcaPatientInfo.getOtherIdNumber ()))
        {
            medicalCardPatientInfo.setCardType (IDTypeEnum.OtherPapers.getEnumCode ());
            medicalCardPatientInfo.setCardCode (pcaPatientInfo.getOtherIdNumber ());
        }
        if (null != pcaPatientInfo.getPassportNumber () || "".equals (pcaPatientInfo.getPassportNumber ()))
        {
            medicalCardPatientInfo.setCardType (IDTypeEnum.Passport.getEnumCode ());
            medicalCardPatientInfo.setCardCode (pcaPatientInfo.getPassportNumber ());
        }
        judge (pcaPatientInfo, medicalCardPatientInfo);*/
        return medicalCardPatientInfo;
    }

    private static void judge (PcaPatientTrasInfoVO pcaPatientInfo, MedicalCardPatientInfoVO medicalCardPatientInfo)
    {
        if (!StringUtils.isEmpty (pcaPatientInfo.getDriverIdNumber ()))
        {
            medicalCardPatientInfo.setCardType (IDTypeEnum.DriverLicense.getEnumCode ());
            medicalCardPatientInfo.setCardCode (pcaPatientInfo.getDriverIdNumber ());
        }
        if (!StringUtils.isEmpty (pcaPatientInfo.getTaiwanIdNumber ()))
        {
            medicalCardPatientInfo.setCardType (IDTypeEnum.TaiwanPass.getEnumCode ());
            medicalCardPatientInfo.setCardCode (pcaPatientInfo.getTaiwanIdNumber ());
        }
        if (!StringUtils.isEmpty (pcaPatientInfo.getHkIdNumber ()))
        {
            medicalCardPatientInfo.setCardType (IDTypeEnum.HongKongAndMacaoPass.getEnumCode ());
            medicalCardPatientInfo.setCardCode (pcaPatientInfo.getHkIdNumber ());
        }
        if (!StringUtils.isEmpty (pcaPatientInfo.getOfficerNumber ()))
        {
            medicalCardPatientInfo.setCardType (IDTypeEnum.CertificateOfOfficers.getEnumCode ());
            medicalCardPatientInfo.setCardCode (pcaPatientInfo.getOfficerNumber ());
        }
        if (!StringUtils.isEmpty (pcaPatientInfo.getIdNumber ()))
        {
            medicalCardPatientInfo.setCardType (IDTypeEnum.IdentityCard.getEnumCode ());
            medicalCardPatientInfo.setCardCode (pcaPatientInfo.getIdNumber ());
        }

    }

    /**
     * AttendanceCardPatientInfo 变成 PcaPatientInfo
     * 
     * @param pcaPatientTrasInfo
     * @return
     */
    public static PcaPatientInfo turnToPcaPatientInfo (MedicalCardPatientInfoVO patientInfo)

    {
        PcaPatientInfo pcaPatientInfo = new PcaPatientInfo ();
        pcaPatientInfo.setPatientId (patientInfo.getPatientId ());
        pcaPatientInfo.setName (patientInfo.getName ());
        pcaPatientInfo.setNamePhonetic (patientInfo.getNamePhonetic ());
        pcaPatientInfo.setSexCode (patientInfo.getSexCode ());
        pcaPatientInfo.setDateOfBirth (patientInfo.getDateOfBirth ());
        pcaPatientInfo.setPhoneNumber1 (patientInfo.getPhoneNumber1 ());
        pcaPatientInfo.setHomeAddrZipCode (patientInfo.getHomeAddrZipCode ());
        pcaPatientInfo.setNationCode (patientInfo.getNationCode ());
        pcaPatientInfo.setCitizenshipCode (patientInfo.getCitizenshipCode ());
        pcaPatientInfo.setOccupationCode (patientInfo.getOccupationCode ());
        pcaPatientInfo.setPhoneNumber2 (patientInfo.getPhoneNumber2 ());
        pcaPatientInfo.setHomeAddrProvinceCode (patientInfo.getHomeAddrProvinceCode ());
        pcaPatientInfo.setHomeAddrCityCode (patientInfo.getHomeAddrCityCode ());
        pcaPatientInfo.setHomeAddrCountyCode (patientInfo.getHomeAddrCountyCode ());
        pcaPatientInfo.setHomeAddrStreet (patientInfo.getHomeAddrStreet ());
        pcaPatientInfo.setBirthAddrProvinceCode (patientInfo.getBirthAddrProvinceCode ());
        pcaPatientInfo.setBirthAddrCityCode (patientInfo.getBirthAddrCityCode ());
        pcaPatientInfo.setBirthAddrCountyCode (patientInfo.getBirthAddrCountyCode ());
        pcaPatientInfo.setBirthAddrStreet (patientInfo.getBirthAddrStreet ());
        pcaPatientInfo.setNextOfKin (patientInfo.getNextOfKin ());
        pcaPatientInfo.setNextOfKinPhone1 (patientInfo.getNextOfKinPhone1 ());
        pcaPatientInfo.setRelationshipCode (patientInfo.getRelationshipCode ());
        pcaPatientInfo.setNextOfKinZipCode (patientInfo.getNextOfKinZipCode ());
        pcaPatientInfo.setCreateTime (patientInfo.getCreateTime ());
        pcaPatientInfo.setInsuranceTypeCode (patientInfo.getInsuranceTypeCode ());
        pcaPatientInfo.setPaymentCode (patientInfo.getPaymentCode ());

        // 设置证件号
        if (IDTypeEnum.IdentityCard.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            pcaPatientInfo.setIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.Passport.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            pcaPatientInfo.setPassportNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.CertificateOfOfficers.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            pcaPatientInfo.setOfficerNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.HongKongAndMacaoPass.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            pcaPatientInfo.setHkIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.DriverLicense.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            pcaPatientInfo.setDriverIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.TaiwanPass.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            pcaPatientInfo.setTaiwanIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.OtherPapers.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            pcaPatientInfo.setOtherIdNumber (patientInfo.getCardCode ());
        }
        return pcaPatientInfo;
    }

    /**
     * 得到接口所用的操作信息
     * 
     * @param attendanceCard
     * @return
     */
    public static MsgLog getPcaMsgLog (MedicalCardPatientInfoVO patientInfo)

    {
        MsgLog log = new MsgLog ();
        log.setIpAddr (patientInfo.getIpAddr ());
        log.setCause (patientInfo.getCause ());
        log.setOpApp (patientInfo.getOpApp ());
        log.setOpBy (patientInfo.getOpApp ());
        log.setOpByName (patientInfo.getOpByName ());
        log.setRequestWay (patientInfo.getRequestWay ());
        log.setTerminal (patientInfo.getTerminal ());
        return log;
    }

    /**
     * 把PCA接口返回的病人卡的信息，转成PTS需要的卡信息
     * 
     * @param pcaMediaTrasInfo
     * @return
     */
    public static MedicalCardInfoVO getAboutCardInfo (PcaMediaTrasInfo pcaMediaTrasInfo)
    {
        MedicalCardInfoVO aboutCardInfo = new MedicalCardInfoVO ();
        aboutCardInfo.setCardNo (pcaMediaTrasInfo.getIdentityNumber ());
        aboutCardInfo.setCardName (pcaMediaTrasInfo.getIdentityTypeName ());
        aboutCardInfo.setPatientId (pcaMediaTrasInfo.getPatientId ());
        aboutCardInfo.setCodeType (pcaMediaTrasInfo.getIdentityType ());
        aboutCardInfo.setCardStatue (pcaMediaTrasInfo.getMediaStatus ());
        return aboutCardInfo;
    }

    /**
     * 转换查询条件
     * 
     * @param patientInfo 存储查询条件：姓名，证件类型，证件号
     * @return
     */
    public static PatientSearch getSearchConditions (MedicalCardPatientInfoVO patientInfo)
    {
        PatientSearch patientSearch = new PatientSearch ();
        patientSearch.setName (patientInfo.getName ());
        patientSearch.setPhoneNumber (patientInfo.getPhoneNumber1 ());
        // 设置证件号
        if (IDTypeEnum.IdentityCard.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            patientSearch.setIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.Passport.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            patientSearch.setPassportNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.CertificateOfOfficers.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            patientSearch.setOfficerNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.HongKongAndMacaoPass.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            patientSearch.setHkIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.DriverLicense.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            patientSearch.setDriverIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.TaiwanPass.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            patientSearch.setTaiwanIdNumber (patientInfo.getCardCode ());
        }
        if (IDTypeEnum.OtherPapers.getEnumCode ().equals (patientInfo.getCardType ()))
        {
            patientSearch.setOtherIdNumber (patientInfo.getCardCode ());
        }
        return patientSearch;
    }

    /**
     * 转换验卡返回结果
     */
    public static MedicalCardInfoVO getResult (Result result)
    {
        MedicalCardInfoVO attendanceCardInfo = new MedicalCardInfoVO ();
        attendanceCardInfo.setCardDesc (result.getDesc ());
        attendanceCardInfo.setCardStatue (result.getCode ());
        attendanceCardInfo.setCardNo (result.getCardNo ());
        return attendanceCardInfo;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
